<?php

namespace App\Http\Controllers\H5;

use App\Services\H5\IndexService;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    //入口

    public function __construct(IndexService $service)
    {
        $this->db = $service;
    }

    /**
     * 更新H5信息
     */
    public function toH5code(Request $request)
    {
        //字段验证
        $this->validate($request, [
            'code' => 'required|min:4',
            'h5code' => 'required',
        ], [
            'code.required' => '码值不能空',
            'code.min' => '码值不能小于4位',
            'h5code.required' => 'H5编号不能空',
        ]);
        $code = $request->post('code');
        $h5code = $request->post('h5code');
        $res = $this->db->toH5code($code, $h5code); //H5信息
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $arys = [
            'code' => $code,
            'h5code' => $h5code,
            'code_used' => $res['data']['code_used'],
        ];
        return $this->toJson(0, '', $arys);
    }

    /**
     * 设置手机号
     */
    public function setPhone(Request $request)
    {
        //字段验证
        $this->validate($request, [
            'h5code' => 'required',
            'phone' => 'required|regex:/^1[34578]\d{9}$/',
        ], [
            'h5code.required' => 'H5编号不能空',
            'phone.required' => '手机号是必须的',
            'phone.regex' => '手机号格式错误',
        ]);
        $h5code = $request->post('h5code', '');
        $phone = $request->post('phone', '');
        //设置登录
        $rst = $this->db->setPhone($phone, $h5code);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson();
    }

    /**
     * 场景列表
     */
    public function sceneIndex(Request $request)
    {
        $pages['start'] = $request->post('pageStart', 1);
        $pages['limit'] = $request->post('pageLimit', 10);
        $res = $this->db->sceneIndex($pages);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 场景内容列表
     */
    public function sceneCntIndex(Request $request)
    {
        $scene_id = $request->post('scene_id', 0);
        $res = $this->db->sceneCntIndex($scene_id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}